package com.yingsheng.bi.hks.service.goods;

import com.yingsheng.bi.hks.basic.PUser;
import com.yingsheng.bi.hks.basic.enums.EnumConst;
import com.yingsheng.bi.hks.basic.utils.Subject;
import com.yingsheng.bi.hks.basic.utils.Utils;
import com.yingsheng.bi.hks.bo.GoodsBo;
import com.yingsheng.bi.hks.bo.GoodsDetailBo;
import com.yingsheng.bi.hks.bo.GoodsOrderBo;
import com.yingsheng.bi.hks.bo.spec.SpecItemBo;
import com.yingsheng.bi.hks.bo.spec.SpecValueBo;
import com.yingsheng.bi.hks.dto.goods.*;
import com.yingsheng.bi.hks.mapping.GoodsDetailMappings;
import com.yingsheng.bi.hks.mapping.GoodsMappings;
import com.yingsheng.bi.hks.mapping.GoodsMgmtMappings;
import com.yingsheng.bi.hks.repository.dao.po.*;
import com.yingsheng.bi.hks.repository.dao.po.ext.GoodsOrder;
import com.yingsheng.bi.hks.repository.dao.service.GoodsDaoService;
import com.yingsheng.bi.hks.repository.dao.service.GoodsDetailDaoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class GoodsMgmtService {

    @Autowired
    private GoodsDaoService goodsDaoService;

    @Autowired
    private GoodsDetailDaoService goodsDetailDaoService;

    public GoodsAddRes add(GoodsAddReq req) {

        Goods goods = GoodsMgmtMappings.reqToGoods(req);
        GoodsDetail goodsDetail = GoodsMgmtMappings.reqToGoodsDetail(req);

        goods.setId(Utils.genUUid());
        goods.setState(EnumConst.GoodsState.GoodsState_0);
        goodsDetail.setId(Utils.genUUid());
        goodsDetail.setGoodsId(goods.getId());

        goodsDaoService.insert(goods);
        goodsDetailDaoService.insert(goodsDetail);
        return new GoodsAddRes();
    }

    public GoodsSpecAddRes goodsSpecAdd(GoodsSpecAddReq req) {

        List<GoodsSpec> goodsSpecs = new ArrayList<>();
        List<SpecItem> specItems = new ArrayList<>();
        List<SpecValue> specValues = new ArrayList<>();

        for (int i = 0; i < req.getSpecs().size(); i++) {
            SpecItemBo specItemBo = req.getSpecs().get(i);

            SpecItem specItem = new SpecItem();
            specItem.setId(Utils.genUUid());
            specItem.setName(specItemBo.getName());
            for (int j = 0; j < specItemBo.getValueBos().size(); j++) {
                SpecValueBo specValueBo = specItemBo.getValueBos().get(j);
                SpecValue value = new SpecValue();
                value.setId(Utils.genUUid());
                value.setSpecId(specItem.getId());
                value.setValue(specValueBo.getValue());
                specValues.add(value);
            }
            GoodsSpec goodsSpec = new GoodsSpec();
            goodsSpec.setId(Utils.genUUid());
            goodsSpec.setSpecValue(specItem.getId());
            goodsSpec.setGoodsId(req.getGoodsId());
            goodsSpec.setImages(specItemBo.getImages());
            goodsSpecs.add(goodsSpec);
            specItems.add(specItem);
        }
        goodsDaoService.goodsSpecAdd(goodsSpecs, specItems, specValues);
        return new GoodsSpecAddRes();
    }


    public GoodsSpecQryRes goodsSpecQry(GoodsSpecQryReq req) {
        GoodsSpecQryRes res = new GoodsSpecQryRes();
        List<GoodsSpec> goodsSpecs = goodsDaoService.selectSpecItemByGoodsId(req.getGoodsId());
        if (goodsSpecs.isEmpty()) {
            return res;
        }
        List<SpecItemBo> specItemBos = new ArrayList<>();
        for (int i = 0; i < goodsSpecs.size(); i++) {

            SpecItemBo specItemBo = new SpecItemBo();
            List<SpecValueBo> specValueBos = new ArrayList<>();
            SpecItem specItem = goodsDaoService.selectSpecItemById(goodsSpecs.get(i).getSpecValue());
            List<SpecValue> specValues = goodsDaoService.selectSpecValuesByItemId(specItem.getId());
            for (int j = 0; j < specValues.size(); j++) {
                SpecValue specValue = specValues.get(j);
                SpecValueBo specValueBo = new SpecValueBo();
                specValueBo.setValue(specValue.getValue());
                specValueBos.add(specValueBo);
            }
            specItemBo.setName(specItem.getName());
            specItemBo.setImages(goodsSpecs.get(i).getImages());
            specItemBo.setValueBos(specValueBos);
            specItemBos.add(specItemBo);
        }
        res.setGoodsId(req.getGoodsId());
        res.setSpecs(specItemBos);
        return res;
    }

    public GoodsQryRes goodsQry(GoodsQryReq req) {
        List<Goods> goodsList = goodsDaoService.qry(req,"0");
        List<GoodsBo> goodsBoList = GoodsMappings.INSTANCE.goodsList2boList(goodsList);
        GoodsQryRes res = new GoodsQryRes();
        res.setGoodsBoList(goodsBoList);
        return res;
    }

    public GoodsDetailQryRes goodsDetailQry(GoodsDetailQryReq req) {
        Goods goods = goodsDaoService.selectGoodsByIdAndCheckNoNull(req.getGoodsId());
        GoodsBo goodsBo = GoodsMappings.INSTANCE.goods2GoodsBo(goods);
        List<GoodsDetail> goodsList = goodsDetailDaoService.qry(req);
        List<GoodsDetailBo> goodsBoList = GoodsDetailMappings.INSTANCE.goodsList2boList(goodsList);
        GoodsDetailQryRes res = new GoodsDetailQryRes();
        res.setGoodsBo(goodsBo);
        res.setGoodsDetailBo(goodsBoList.get(0));
        return res;
    }

    public GoodsOrderQryRes goodsOrderQry(GoodsOrderQryReq req) {
        PUser user = Subject.get();
        GoodsOrderQryRes res = new GoodsOrderQryRes();
        List<GoodsOrder> goodsOrders = goodsDaoService.selectGoodsOrderByUserId(user.getId(),EnumConst.GoodsOrderState.state_0);
        List<GoodsOrderBo> goodsOrderBos = GoodsMappings.INSTANCE.goodsOrderList2boList(goodsOrders);
        res.setGoodsOrderBos(goodsOrderBos);
        return res;
    }
}
